<template>
  <div id="app">
    <el-row>
      <el-col :span="24">
        <el-card class="box-card">
          <template #header>
            <div class="card-header">
              <span>城市列表</span>
              <el-button type="primary" @click="handleAddCity">添加城市</el-button>
            </div>
          </template>
          <el-table :data="cities" border style="width: 100%">
            <el-table-column prop="cityName" label="城市名称">
            </el-table-column>
            <el-table-column prop="provinceName" label="所属省份">
            </el-table-column>
<!--            <el-table-column label="操作" width="180">-->
<!--              <template #default="scope">-->
<!--                <el-button type="primary" size="small" @click="handleEditCity(scope.row)">编辑</el-button>-->
<!--                <el-button type="danger" size="small" @click="handleDeleteCity(scope.row)">删除</el-button>-->
<!--              </template>-->
<!--            </el-table-column>-->
          </el-table>
        </el-card>
      </el-col>
    </el-row>

    <el-dialog v-model="dialogVisible" title="添加城市" width="30%">
      <el-form :model="form" label-width="80px">
        <el-form-item label="城市名称">
          <el-input v-model="form.cityName" />
        </el-form-item>
        <el-form-item label="所属省份">
          <el-select v-model="form.provinceCode" placeholder="请选择省份">
            <el-option
                v-for="province in provinces"
                :key="province.provinceCode"
                :label="province.provinceName"
                :value="province.provinceCode"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSaveCity">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script>
import axios from 'axios';
import { ElMessage } from 'element-plus';

export default {
  name: 'AdminCity',
  data() {
    return {
      cities: [],
      provinces: [],
      dialogVisible: false,
      form: {
        cityName: '',
        provinceCode: null,
      },
    };
  },
  mounted() {
    this.fetchCities();
    this.fetchProvinces();
  },
  methods: {
    fetchCities() {
      axios.get('/cities')
          .then(response => {
            this.cities = response.data;
          })
          .catch(error => {
            console.error('获取城市列表失败', error);
            ElMessage.error('获取城市列表失败');
          });
    },
    fetchProvinces() {
      axios.get('/provinces')
          .then(response => {
            this.provinces = response.data;
          })
          .catch(error => {
            console.error('获取省份列表失败', error);
            ElMessage.error('获取省份列表失败');
          });
    },
    handleAddCity() {
      this.dialogVisible = true;
      this.form = {
        cityName: '',
        provinceCode: null,
      };
    },
    handleSaveCity() {
      const cityDto = {
        cityName: this.form.cityName,
        provinceCode: this.form.provinceCode,
      };
      axios.post('/cities', cityDto)
          .then(() => {
            this.dialogVisible = false;
            this.fetchCities();
            ElMessage.success('添加城市成功');
          })
          .catch(error => {
            console.error('添加城市失败', error);
            ElMessage.error('添加城市失败');
          });
    },
    // handleEditCity(city) {
    //   // TODO: 实现编辑城市功能
    //   console.log('编辑城市', city);
    // },
    // handleDeleteCity(city) {
    //   // TODO: 实现删除城市功能
    //   console.log('删除城市', city);
    // },
  },
};
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}

.box-card {
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
